package com.sense.kukoo.biz_media.view

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.gyf.immersionbar.ImmersionBar
import com.sense.kukoo.biz_media.R
import com.sense.kukoo.biz_media.adapter.MediaHistoryAdapter
import com.sense.kukoo.biz_media.databinding.BizMediaActivityMediaHistoryBinding
import com.sense.kukoo.biz_media.vm.MediaHistoryViewModel
import com.sense.kukoo.common.anim.BottomMotionAnimHelper
import com.sense.kukoo.common.constant.ARouterUrl
import com.sense.kukoo.common.media.watcher.UserWatcher
import com.sense.kukoo.common.mvvm.view.BaseMvvmActivity
import com.sense.kukoo.common.repo.biz.MediaHistoryItemBean
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.wiget.dialog.MiddleConfigDialog
import com.sense.kukoo.common.wiget.state.NetworkErrorState
import com.zy.multistatepage.state.SuccessState

@Route(path = ARouterUrl.ROUTE_MEDIA_MODULE_MEDIA_HISTORY_ACTIVITY)
class MediaHistoryActivity: BaseMvvmActivity<MediaHistoryViewModel, BizMediaActivityMediaHistoryBinding>() {

    private val TAG: String = javaClass.simpleName
    private val mAnimHelper = BottomMotionAnimHelper()
    private var mCurrentMusicState: Int = UserWatcher.MusicStateWatcher.STATE_NOT_PLAYING
    private var mCurrentMusicProgress: Int = 1

    private lateinit var mAdapter: MediaHistoryAdapter

    override fun responseVM(action: Int) {}

    override fun getLayoutId(): Int = R.layout.biz_media_activity_media_history

    override fun getViewModelClass(): Class<MediaHistoryViewModel> = MediaHistoryViewModel::class.java

    override fun initStatusBar() {
        ImmersionBar.with(this)
            .transparentStatusBar()
            .navigationBarColor(com.sense.kukoo.common_design.R.color.bg_background)
            .statusBarDarkFont(true)
            .navigationBarDarkIcon(true)
            .fitsSystemWindows(false)
            .init()
    }

    override fun initData() {
        mBinding.lifecycleOwner = this
        mBinding.vm = mViewModel
        //
        bindAction(mViewModel.action_back){
            mAnimHelper.animateToFinish()
        }
        bindAction(mViewModel.action_finishRefresh){
            mBinding.refreshLayout.finishRefresh()
        }
        bindAction(mViewModel.action_finishLoadMore){
            mBinding.refreshLayout.finishLoadMore()
        }
        bindAction(mViewModel.action_showCleanAllDialog){
            showCleanAllDialog()
        }
        bindAction(mViewModel.action_networkError){
            mBinding.multiStateContainer.show<NetworkErrorState> {
                it.onRetry {
                    mViewModel.onErrorRetry()
                }
            }
        }
        bindData(mViewModel.data_enableLoadMore){
            mBinding.refreshLayout.setEnableLoadMore(it)
        }
        bindData(mViewModel.data_listenPage){
            mBinding.multiStateContainer.show<SuccessState>()
            if(it.isEmpty()){
                mBinding.tvBtnCleanAll.visibility = View.GONE
            } else {
                mBinding.tvBtnCleanAll.visibility = View.VISIBLE
            }
            mAdapter.list = it
            mAdapter.notifyDataSetChanged()
        }
        bindData(mViewModel.data_currentMusic){
            mAdapter.setCurrentMusic(it)
        }
        bindData(mViewModel.data_currentPercent){
            mCurrentMusicProgress = it
            mAdapter.setCurrentPlayState(mCurrentMusicState, mCurrentMusicProgress)
        }
        bindData(mViewModel.data_currentState){
            mCurrentMusicState = it
            mAdapter.setCurrentPlayState(mCurrentMusicState, mCurrentMusicProgress)
        }
    }

    override fun initView() {
        //
        mAdapter = MediaHistoryAdapter(arrayListOf(), mListener)
        mBinding.recyclerView.adapter = mAdapter

        val linearLayoutManager = LinearLayoutManager(this)
        mBinding.recyclerView.layoutManager = linearLayoutManager
        mBinding.refreshLayout.setOnRefreshListener {
            //OnRefresh
            mViewModel.refreshListenPage()
        }
        mBinding.refreshLayout.setOnLoadMoreListener {
            //OnLoadMore
            mViewModel.loadMoreListenPage()
        }
        //弹出动画
        val first = ApplicationContext.dp2px(800F).toFloat()
        mAnimHelper.init(mBinding.flMediaHistoryRoot, mBinding.clMediaHistoryContentRoot, first){ finish() }
        mAnimHelper.animateToStart()
        //
        mViewModel.initData()
    }

    override fun onBackPressed() {
        mAnimHelper.animateToFinish()
    }

    fun showCleanAllDialog(){
        val dialog = MiddleConfigDialog(this)
        val mListener = object : MiddleConfigDialog.MiddleConfigListener {
            override fun onLeftClick() {
                dialog.dismiss()
            }

            override fun onRightClick() {
                dialog.dismiss()
                mViewModel.onMediaDeleteAll()
            }
        }
        dialog
            .setTitleStr(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.history_clean_all_title))
            .setMsgStr(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.history_clean_all_content))
            .setBtnLeft(
                    ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.settings_cache_left_btn),
                    this.getResources()
                        .getColor(com.sense.kukoo.common_design.R.color.text_active)
            )
            .setBtnRight(
                    ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.history_clean_all_remove),
                    this.getResources()
                        .getColor(com.sense.kukoo.common_design.R.color.fg_error)
            )
            .setListener(mListener)
            .show()
    }

    private val mListener = object :MediaHistoryAdapter.MediaHistoryEventListener{
        override fun onMediaPlayClick(bean: MediaHistoryItemBean) {
            mViewModel.onMediaPlayClick(bean)
        }

        override fun onMediaDelete(bean: MediaHistoryItemBean) {
            mViewModel.onMediaDelete(bean)
        }
    }
}